<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" >
      <el-form-item label="单位" prop="unitId">
        <el-select filterable v-model="queryParams.unitId" placeholder="请选择单位" style="width: 200px">
          <el-option
            v-for="item in unitList"
            :key="item.id"
            :label="item.unitName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="联网单位建筑物" prop="buildingId">-->
<!--        <el-input-->
<!--          v-model="queryParams.buildingId"-->
<!--          placeholder="请输入联网单位建筑物"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="建构筑物名称" prop="bdName">
        <el-input
          v-model="queryParams.bdName"
          placeholder="请输入建构筑物名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:building:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:building:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:building:remove']"
          v-hasRole="['admin']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:building:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="buildingList" @selection-change="handleSelectionChange"
              :row-class-name="tableRowClassName">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="单位" align="center" prop="unit.unitName" />
<!--      <el-table-column label="联网单位建筑物" align="center" prop="buildingId" />-->
      <el-table-column label="建筑物名称" align="center" prop="bdName" />
      <el-table-column label="建筑地址信息" align="center" prop="bdAddress" />
<!--      <el-table-column label="建、构筑物类别,通过建筑基础信息获取" align="center" prop="bdTypeId" />-->
<!--      <el-table-column label="使用性质,通过建筑基础信息获取" align="center" prop="bdUseWayId" />-->
<!--      <el-table-column label="结构类型,通过建筑基础信息获取" align="center" prop="bdStructureId" />-->
<!--      <el-table-column label="建筑使用层数" align="center" prop="bdUseFloor" />-->
<!--      <el-table-column label="耐火等级,通过建筑基础信息获取" align="center" prop="bdFireResistantId" />-->
<!--      <el-table-column label="建筑高度 单位：m，精确到小数点后2位" align="center" prop="bdHeight" />-->
<!--      <el-table-column label="建筑面积 单位：m2" align="center" prop="bdSpace" />-->
<!--      <el-table-column label="建筑安全距离" align="center" prop="bdSafeDistance" />-->
<!--      <el-table-column label="占地面积 单位：m2" align="center" prop="bdOccupySpace" />-->
<!--      <el-table-column label="标准层面积 单位：m2" align="center" prop="bdStdFloorSpace" />-->
<!--      <el-table-column label="地上层数" align="center" prop="bdOverGroundFloor" />-->
<!--      <el-table-column label="地上层面积 单位：m2" align="center" prop="bdOverGroundSpace" />-->
<!--      <el-table-column label="地下层数" align="center" prop="bdUnderGroundFloor" />-->
<!--      <el-table-column label="地下层面积 单位：m2" align="center" prop="bdUnderGroundSpace" />-->
      <el-table-column label="建造日期" align="center" prop="bdFinishDate" />
      <el-table-column label="推送状态" align="center" prop="pushStatus" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.push_status" :value="scope.row.pushStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.pushStatus ==='2'"
                     size="mini"
                     type="text"
                     icon="el-icon-bottom"
                     @click="handleCancel(scope.row)"
                     v-hasPermi="['system:unit:edit']"
                     v-hasRole="['admin']"
          >取消推送
          </el-button>
          <el-button v-if="scope.row.pushStatus !=='2'"
            size="mini"
            type="text"
            icon="el-icon-top"
            @click="handleSort(scope.row)"
            v-hasPermi="['system:unit:edit']"
          >推送
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:building:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:building:remove']"
            style="color: #ff0000;border: none"
            v-hasRole="['admin']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改建筑物对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="150px" inline>
        <el-form-item label="单位" prop="unitId">
          <el-select v-model="form.unitId" placeholder="请选择单位" style="width: 200px">
            <el-option
              v-for="item in unitList"
              :key="item.id"
              :label="item.unitName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="建筑物名称" prop="bdName">
          <el-input v-model="form.bdName" placeholder="请输入建、构筑物名称" />
        </el-form-item>
        <el-form-item label="建筑地址信息" prop="bdAddress">
          <el-input v-model="form.bdAddress" placeholder="请输入建筑地址信息" />
        </el-form-item>
        <el-form-item label="建筑物类别" prop="bdTypeId">
          <el-select v-model="form.bdTypeId" placeholder="请选择建筑物类别" style="width: 200px">
            <el-option
              v-for="item in buildingTypeList"
              :key="item.id"
              :label="item.buildingTypeName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="使用性质" prop="bdUseWayId">
          <el-select v-model="form.bdUseWayId" placeholder="请选择使用性质" style="width: 200px">
            <el-option
              v-for="item in buildingUseWayList"
              :key="item.id"
              :label="item.buildingUseWayName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="结构类型" prop="bdStructureId">
          <el-select v-model="form.bdStructureId" placeholder="请选择结构类型" style="width: 200px">
            <el-option
              v-for="item in buildingStructureList"
              :key="item.id"
              :label="item.buildingStructureName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="耐火等级" prop="bdFireResistantId">
          <el-select v-model="form.bdFireResistantId" placeholder="请选择耐火等级" style="width: 200px">
            <el-option
              v-for="item in buildingResistantList"
              :key="item.id"
              :label="item.fireResistantName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="火灾危险性" prop="bdFireHazardId">
          <el-select v-model="form.bdFireHazardId" placeholder="请选择火灾危险性" style="width: 200px">
            <el-option
              v-for="item in buildingFireHazardList"
              :key="item.id"
              :label="item.fireHazardName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="使用层数" prop="bdUseFloor">
          <el-input v-model="form.bdUseFloor" placeholder="请输入建筑使用层数" />
        </el-form-item>
        <el-form-item label="建筑高度" prop="bdHeight">
          <el-input v-model="form.bdHeight" placeholder="请输入建筑高度 单位：m，精确到小数点后2位" />
        </el-form-item>
        <el-form-item label="建筑面积" prop="bdSpace">
          <el-input v-model="form.bdSpace" placeholder="请输入建筑面积 单位：m2" />
        </el-form-item>
        <el-form-item label="建筑安全距离" prop="bdSafeDistance">
          <el-input v-model="form.bdSafeDistance" placeholder="请输入建筑安全距离 单位：m，精确到小数点后2位" />
        </el-form-item>
        <el-form-item label="占地面积" prop="bdOccupySpace">
          <el-input v-model="form.bdOccupySpace" placeholder="请输入占地面积 单位：m2" />
        </el-form-item>
        <el-form-item label="标准层面积" prop="bdStdFloorSpace">
          <el-input v-model="form.bdStdFloorSpace" placeholder="请输入标准层面积 单位：m2" />
        </el-form-item>
        <el-form-item label="地上层数" prop="bdOverGroundFloor">
          <el-input v-model="form.bdOverGroundFloor" placeholder="请输入地上层数" />
        </el-form-item>
        <el-form-item label="地上层面积" prop="bdOverGroundSpace">
          <el-input v-model="form.bdOverGroundSpace" placeholder="请输入地上层面积 单位：m2" />
        </el-form-item>
        <el-form-item label="地下层数" prop="bdUnderGroundFloor">
          <el-input v-model="form.bdUnderGroundFloor" placeholder="请输入地下层数" />
        </el-form-item>
        <el-form-item label="地下层面积" prop="bdUnderGroundSpace">
          <el-input v-model="form.bdUnderGroundSpace" placeholder="请输入地下层面积 单位：m2" />
        </el-form-item>
        <el-form-item label="隧道高度" prop="bdTunnelHeight">
          <el-input v-model="form.bdTunnelHeight" placeholder="请输入隧道高度 单位：m，精确到小数点后2位" />
        </el-form-item>
        <el-form-item label="隧道长度" prop="bdTunnelLength">
          <el-input v-model="form.bdTunnelLength" placeholder="请输入隧道长度 单位：m，精确到小数点后2位" />
        </el-form-item>
        <el-form-item label="建造日期" prop="bdFinishDate">
          <el-date-picker clearable style="width: 200px"
                          v-model="form.bdFinishDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择建造日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="避难层数量" prop="bdRefugeNum">
          <el-input v-model="form.bdRefugeNum" placeholder="请输入避难层数量" />
        </el-form-item>
        <el-form-item label="避难层总面积" prop="bdRefugeSpace">
          <el-input v-model="form.bdRefugeSpace" placeholder="请输入避难层总面积 单位：m2" />
        </el-form-item>
        <el-form-item label="避难层位置描述" prop="bdRefugePosition">
          <el-input v-model="form.bdRefugePosition" placeholder="请输入避难层位置描述 最大长度50" />
        </el-form-item>
        <el-form-item label="消防电梯是否使用" prop="bdFireElevatorUse">
          <el-select v-model="form.bdFireElevatorUse" placeholder="请选择消防电梯是否使用" style="width: 200px">
            <el-option label="使用" value="0"/>
            <el-option label="未使用" value="1"/>
          </el-select>
        </el-form-item>
        <el-form-item label="消防电梯数量" prop="bdFireElevatorNum">
          <el-input v-model="form.bdFireElevatorNum" placeholder="请输入消防电梯数量" />
        </el-form-item>
        <el-form-item label="消防电梯容纳总重量" prop="bdFireElevatorCapacity">
          <el-input v-model="form.bdFireElevatorCapacity" placeholder="请输入消防电梯容纳总重量 单位：kg" />
        </el-form-item>
        <el-form-item label="日常工作时间人数" prop="bdNormalPopulation">
          <el-input v-model="form.bdNormalPopulation" placeholder="请输入日常工作时间人数" />
        </el-form-item>
        <el-form-item label="最大容纳人数" prop="bdMaxPopulation">
          <el-input v-model="form.bdMaxPopulation" placeholder="请输入最大容纳人数" />
        </el-form-item>
        <el-form-item label="经度" prop="gpsLng">
          <el-input v-model="form.gpsLng" placeholder="请输入GPS位置，经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="gpsLat">
          <el-input v-model="form.gpsLat" placeholder="请输入GPS位置，纬度" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBuilding,
  getBuilding,
  delBuilding,
  addBuilding,
  updateBuilding,
  buildingType,
  buildingUseWay, buildingStructure, buildingResistant, buildingFireHazard, buildingPush, delBuildingId
} from "@/api/system/building"
import {listUnit} from "@/api/system/unit";

export default {
  name: "Building",
  dicts: ['push_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 建筑物表格数据
      buildingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        unitId: null,
        buildingId: null,
        bdName: null,
        bdAddress: null,
        bdTypeId: null,
        bdUseWayId: null,
        bdStructureId: null,
        bdUseFloor: null,
        bdFireResistantId: null,
        bdHeight: null,
        bdSpace: null,
        bdSafeDistance: null,
        bdOccupySpace: null,
        bdStdFloorSpace: null,
        bdOverGroundFloor: null,
        bdOverGroundSpace: null,
        bdUnderGroundFloor: null,
        bdUnderGroundSpace: null,
        bdTunnelHeight: null,
        bdTunnelLength: null,
        bdFinishDate: null,
        bdRefugeNum: null,
        bdRefugeSpace: null,
        bdRefugePosition: null,
        bdFireElevatorUse: null,
        bdFireElevatorNum: null,
        bdFireElevatorCapacity: null,
        bdNormalPopulation: null,
        bdMaxPopulation: null,
        gpsLng: null,
        gpsLat: null,
        gpsType: null,
        extraData: null,
        pushStatus: null,
        createUserId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        unitId: [
          { required: true, message: "单位不能为空", trigger: "blur" }
        ],
        bdName: [
          { required: true, message: "建筑物名称不能为空", trigger: "blur" }
        ],
        bdAddress: [
          { required: true, message: "建筑地址信息不能为空", trigger: "blur" }
        ],
        bdTypeId: [
          { required: true, message: "建筑物类别不能为空", trigger: "blur" }
        ],
        bdUseWayId: [
          { required: true, message: "使用性质不能为空", trigger: "blur" }
        ],
        bdStructureId: [
          { required: true, message: "结构类型不能为空", trigger: "blur" }
        ],
        bdUseFloor: [
          { required: true, message: "建筑使用层数不能为空", trigger: "blur" }
        ],
      },
      unitList:[],//单位list
      buildingTypeList:[],// 建筑物类型
      buildingUseWayList:[],//   筑物使用性质
      buildingFireHazardList:[],//   筑物火灾危险性
      buildingResistantList:[],//   建筑物耐火等级
      buildingStructureList:[],//   建筑物结构类型

    }
  },
  created() {
    this.getList()
    this.dictList()
    this.getUnit()
  },
  methods: {
    /** 查询建筑物列表 */
    getList() {
      this.loading = true
      listBuilding(this.queryParams).then(response => {
        this.buildingList = response.rows
        this.total = response.total
        this.loading = false
      })

    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        unitId: null,
        buildingId: null,
        bdName: null,
        bdAddress: null,
        bdTypeId: null,
        bdUseWayId: null,
        bdStructureId: null,
        bdUseFloor: null,
        bdFireResistantId: null,
        bdHeight: null,
        bdSpace: null,
        bdSafeDistance: null,
        bdOccupySpace: null,
        bdStdFloorSpace: null,
        bdOverGroundFloor: null,
        bdOverGroundSpace: null,
        bdUnderGroundFloor: null,
        bdUnderGroundSpace: null,
        bdTunnelHeight: null,
        bdTunnelLength: null,
        bdFinishDate: null,
        bdRefugeNum: null,
        bdRefugeSpace: null,
        bdRefugePosition: null,
        bdFireElevatorUse: null,
        bdFireElevatorNum: null,
        bdFireElevatorCapacity: null,
        bdNormalPopulation: null,
        bdMaxPopulation: null,
        gpsLng: null,
        gpsLat: null,
        gpsType: null,
        extraData: null,
        pushStatus: null,
        createUserId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加建筑物"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getBuilding(id).then(response => {
        this.form = response.data
        this.form.bdFireElevatorUse =JSON.stringify(response.data.bdFireElevatorUse)
        this.open = true
        this.title = "修改建筑物"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBuilding(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addBuilding(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除建筑物编号为"' + ids + '"的数据项？').then(function() {
        return delBuilding(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/building/export', {
        ...this.queryParams
      }, `building_${new Date().getTime()}.xlsx`)
    },
    // 获取数据
    dictList() {
      this.getBuildingType()
      this.getBuildingUseWay()
      this.getBuildingFireHazard()
      this.getBuildingResistant()
      this.getBuildingStructure()
    },
    /** 建筑物类型 */
    getBuildingType() {
      buildingType().then(response => {
        this.buildingTypeList = response.data
      })
    },
    /** 筑物使用性质 */
    getBuildingUseWay() {
      buildingUseWay().then(response => {
        this.buildingUseWayList = response.data
      })
    },
    /** 筑物火灾危险性 */
    getBuildingFireHazard() {
      buildingFireHazard().then(response => {
        this.buildingFireHazardList = response.data
      })
    },
    /** 建筑物耐火等级 */
    getBuildingResistant() {
      buildingResistant().then(response => {
        this.buildingResistantList = response.data
      })
    },
    /** 建筑物结构类型 */
    getBuildingStructure() {
      buildingStructure().then(response => {
        this.buildingStructureList = response.data
      })
    },
    /** 推送按钮操作 */
    handleSort(row) {
      const id = row.id
      buildingPush(id).then(response => {
        this.$modal.msgSuccess("推送成功！")
        this.getList()
      })
    },
    /** 取消推送按钮操作 */
    handleCancel(row) {
      const buildingId = row.id
      delBuildingId(buildingId).then(response => {
        this.$modal.msgSuccess("推送成功！")
        this.getList()
      })
    },
    /** 查询单位 */
    getUnit(row) {
      listUnit({
        pageNum: 1,
        pageSize: 999999,
      }).then(response => {
        this.unitList = response.rows

      })
    },
    // 是否推送表格颜色
    tableRowClassName({row}) {
      if (row.pushStatus === '1') {
        return 'warning-row';
      } else if (row.pushStatus === '2') {
        return 'success-row';
      }
      return '';
    },
  }
}
</script>
<style>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>
